package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.igexin.sdk.PushManager;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoFragmentActivity;
import com.jph.takephoto.model.TResult;
import com.wbdgj.R;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.WaitPayActivity;
import com.wbdgj.ui.mine.YhjActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;

import retrofit2.Call;

/**
 * Created by Administrator on 2016/12/21.
 */

public class ZXingActivity extends TakePhotoFragmentActivity {

    private Context context = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ui_activity_zxing);
        CaptureFragment captureFragment = new CaptureFragment();
        captureFragment.setAnalyzeCallback(analyzeCallback);
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_zxing_container, captureFragment).commit();
    }

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Log.e("zp","result="+result);
            if (result.contains("/")){
                String[] split = result.split("/");//以逗号分割
                String p_key = split[0];
                String branch_cloud_id = split[1];
                getCodeCoupon(p_key, branch_cloud_id);
            }else{
                ToastUtils.toastShort(result);
//                startActivityForResult(new Intent(context, WaitPayActivity.class).putExtra("result", result), 1);
            }
//            Intent mIntent = new Intent();
//            mIntent.setClass(context, WaitPayActivity.class);
//            mIntent.putExtra("result", result);
//            // 设置结果，并进行传送
//            setResult(2, mIntent);
//            finish();
        }

        @Override
        public void onAnalyzeFailed() {
//            Intent resultIntent = new Intent();
//            Bundle bundle = new Bundle();
//            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
//            bundle.putString(CodeUtils.RESULT_STRING, "");
//            resultIntent.putExtras(bundle);
//            CaptureActivity.this.setResult(RESULT_OK, resultIntent);
//            CaptureActivity.this.finish();
        }
    };

    private void getCodeCoupon(String P_KEY, String branch_id) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getCodeCoupon(branch_id, P_KEY,  BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, userModelBaseResultModel.getResultMsg(), 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }else{
                    final ConfirmDialog confirmDialog = new ConfirmDialog(context, userModelBaseResultModel.getResultMsg(), 8);
                    confirmDialog.show();
                    confirmDialog.setCanceledOnTouchOutside(false);
                    confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                        @Override
                        public void doConfirm() {
                            confirmDialog.dismiss();
                        }

                        @Override
                        public void doCancel() {
                            confirmDialog.dismiss();
                        }
                    });
                }
            }
        });
    }

    public void back(View view) {
        onBackPressed();
    }

    boolean isLightOn;
    Camera m_Camera;

    public void flashLight(View view) {
        isLightOn = !isLightOn;
        CodeUtils.isLightEnable(isLightOn);
    }

    public void choosePhoto(View view) {
        TakePhoto takePhoto = getTakePhoto();
        takePhoto.onPickMultiple(1);
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void takeCancel() {
        super.takeCancel();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (m_Camera != null) {
            m_Camera.stopPreview();
            m_Camera.release();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == -1){
            finish();
        }
    }
}
